// Angular Version 6 below need to be deleted {providedIn: 'root'}

// angular
import {Injectable} from '@angular/core';
import {HttpParams} from '@angular/common/http';
// owner
import {Request} from 'pengesoft-ng-lib';
// models
// import {MemoryFile} from '../domains/memoryfile.domain';
import {AreaList} from '../domains/arealist.domain';

/**
 *
 */
@Injectable({
  providedIn: 'root'
})
export class CommonServiceSvr {
  private readonly baseUrl: string;

  constructor(
    private request: Request
  ) {
    this.baseUrl = 'commonServiceSvr.assx/';
  }


  getChildren(token: string, id: number): Promise<AreaList> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('id', id.toString());
    return this.request.post<AreaList>(this.baseUrl + 'getChildren', httpParams).then((res) => {
      return new AreaList(res);
    });
  }


  download(targetUrl: string): Promise<void> {
    const httpParams = new HttpParams()
      .append('targetUrl', targetUrl.toString());
    return this.request.post<void>(this.baseUrl + 'download', httpParams).then((res) => {
      return res;
    });
  }


  // uploadPic(pic: MemoryFile): Promise<string> {
  //   const httpParams = new FormData();
  //   httpParams.append('pic', pic);
  //   return this.request.post<string>(this.baseUrl + 'uploadPic', httpParams).then((res) => {
  //     return res;
  //   });
  // }


  // upload(file: MemoryFile): Promise<string> {
  //   const httpParams = new FormData();
  //   httpParams.append('file', file);
  //   return this.request.post<string>(this.baseUrl + 'upload', httpParams).then((res) => {
  //     return res;
  //   });
  // }


  uploadAvatar(avatar: File): Promise<string> {
    const httpParams = new FormData();
    httpParams.append('avatar', avatar);
    return this.request.post<string>(this.baseUrl + 'uploadAvatar', httpParams).then((res) => {
      return res;
    });
  }


  GetVersion(): Promise<string> {
    const httpParams = new HttpParams();
    return this.request.post<string>(this.baseUrl + 'GetVersion', httpParams).then((res) => {
      return res;
    });
  }


  sendCode(phoneOrEmail: string, flag: number): Promise<void> {
    const httpParams = new HttpParams()
      .append('phoneOrEmail', phoneOrEmail.toString())
      .append('flag', flag.toString());
    return this.request.post<void>(this.baseUrl + 'sendCode', httpParams).then((res) => {
      return res;
    });
  }


  getAreaTree(token: string): Promise<AreaList> {
    const httpParams = new HttpParams()
      .append('token', token.toString());
    return this.request.post<AreaList>(this.baseUrl + 'getAreaTree', httpParams).then((res) => {
      return new AreaList(res);
    });
  }


  // uploadDoc(doc: MemoryFile): Promise<string> {
  //   const httpParams = new FormData();
  //   httpParams.append('doc', doc);
  //   return this.request.post<string>(this.baseUrl + 'uploadDoc', httpParams).then((res) => {
  //     return res;
  //   });
  // }
}
